﻿using System;
using System.Web;
using System.Web.Mvc;
using com.hyrentals.common.image;

namespace com.hyrentals.imageserver.Controllers
{
    public class ImageController : Controller
    {
        public bool IsCacheValid()
        {
            DateTime utcNow = DateTime.UtcNow;
            DateTime expire = utcNow.AddYears(10);

            string lastModifiedTimeStr = Request.Headers["If-Modified-Since"];
            if (string.IsNullOrEmpty(lastModifiedTimeStr))
            {
                Response.AppendHeader("Last-Modified", utcNow.ToString("r"));
                Response.AppendHeader("Expires", expire.ToString("r"));
                Response.AppendHeader("Cache-Control", "max-age=315360000");
                return false;
            }

            DateTime lastModifiedTime = DateTime.MaxValue;
            DateTime lmt;

            if (!DateTime.TryParse(lastModifiedTimeStr, out lmt))
            {
                Response.AppendHeader("Last-Modified", utcNow.ToString("r"));
                Response.AppendHeader("Expires", expire.ToString("r"));
                Response.AppendHeader("Cache-Control", "max-age=315360000");
                return false;
            }

            lastModifiedTime = lmt.ToUniversalTime();
            if (lastModifiedTime.AddSeconds(315360000d) <= utcNow)
            {
                Response.AppendHeader("Last-Modified", utcNow.ToString("r"));
                Response.AppendHeader("Expires", expire.ToString("r"));
                Response.AppendHeader("Cache-Control", "max-age=315360000");
                return false;
            }

            Response.StatusCode = 304;
            Response.StatusDescription = "Not Modified";
            Response.AppendHeader("Expires", lastModifiedTime.AddYears(10).ToString("r"));
            Response.AppendHeader("Cache-Control", "max-age=315360000");
            return true;
        }
        
        [HttpGet]
        public ActionResult Download(string id, int width, int height, byte process)
        {
            if (width <= 0 || width > 1000 || height <= 0 || height > 1000)
            {
                throw new HttpException(404, "Image not found");
            }
            if (IsCacheValid())
            {
                return Content(string.Empty);
            }
            else
            {
                ImageProcessEnum imageProcessType = (ImageProcessEnum)process;
                if (imageProcessType == ImageProcessEnum.Fill)
                {
                    byte[] bytes = ImageManagerService.GetFillImage(id, width, height);
                    if (bytes == null)
                    {
                        throw new HttpException(404, "Image not found");
                    }
                    return File(bytes, "image/jpeg");
                }
                else if (imageProcessType == ImageProcessEnum.Cut)
                {
                    byte[] bytes = ImageManagerService.GetCutImage(id, width, height);
                    if (bytes == null)
                    {
                        throw new HttpException(404, "Image not found");
                    }
                    return File(bytes, "image/jpeg");
                }
                else if (imageProcessType == ImageProcessEnum.Adaptive)
                {
                    byte[] bytes = ImageManagerService.GetAdaptiveImage(id, width, height);
                    if (bytes == null)
                    {
                        throw new HttpException(404, "Image not found");
                    }
                    return File(bytes, "image/jpeg");
                }
                else
                    throw new HttpException(404, "Image not found");
            }
        }

    }
}